<template>
  <div>
    <el-upload
      multiple
      name="myfile"
      ref="receivingFile"
      class="upload-demo"
      :auto-upload="false"
      :data="uplodaData"
      :headers="headers"
      :on-preview="handlePreview"
      :action="uplodaUrl"
      :on-remove="handleRemove"
      :file-list="fileUploads"
      v-if="nowStep==1&&!isNaN(taskMatterId)"
    >
      <el-button size="small" type="primary" icon="el-icon-upload">上传文件</el-button>
    </el-upload>
    <el-upload
      multiple
      name="myfile"
      :action="uplodaUrl"
      :headers="headers"
      :data="uplodaData"
      :on-preview="handlePreview"
      :file-list="fileUploads"
      v-if="nowStep!=1"
    >
      <el-button size="small" type="primary" icon="el-icon-upload" v-if="firstStepFalg">上传文件</el-button>
    </el-upload>
  </div>
</template>

<script>
import { delUpload } from "@/api/task/upload";
import { getToken } from "@/utils/auth";
export default {
  props: {
    uplodaData: {
      type: Object,
      default: {}
    },
    nowStep: {
      type: Number,
      default: 0
    },
    taskMatterId: {
    },
    firstStepFalg: {
      type: Boolean,
      default: true
    },
    fileUploads: {
      type: Array,
      default: function() {
        return [];
      }
    }
  },
  watch: {
    taskMatterId: function() {
      this.setTaskMatterId();
    },
    nowStep:{
      handler(val){
        this.nowStep=val;
      }
    }
  },

  /*  mounted() {
        this.$nextTick(() => {
          this.setTaskMatterId();
        });
      },*/

   created() {
        this.setTaskMatterId();
      },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      uplodaUrl: _cfg.serverUrl + "/task/upload/add"
    };
  },
  methods: {
    handleRemove(val) {
      if (typeof val.id != "undefined") {
        delUpload(val.id).then(response => {});
      }
    },
    //设置任务ID
    async setTaskMatterId() {
      console.log(isNaN(this.taskMatterId),"this.taskMatterId")
      if (this.taskMatterId) {
        this.uplodaData.processid = this.taskMatterId;
      }
    },
    //处理下载
    handlePreview(val) {
      this.downloadFile(val.url);
    },

    uploadFile(id) {
      this.uplodaData.processid = id;
      this.$nextTick(() => {
        if (this.$refs.receivingFile) {
          this.$refs.receivingFile.submit();
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
